package com.szty.mod_jyzs.ui.questionExplain

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.annotation.LongDef
import com.sjh.baseutils.color
import com.sjh.baseutils.loge
import com.szty.lib_pen.oidbluetooth.ColorUtil
import com.szty.lib_pen.oidbluetooth.DotUtils
import com.szty.lib_pen.surfaceview.BasePen
import com.szty.lib_pen.surfaceview.NormalPen
import com.szty.mod_jyzs.R
import com.tqltech.tqlpencomm.bean.Dot

/**
 *@author : created by SunJH
 *时间：2021/8/2 000210
 *作用：
 */
class EraserPenView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) {
    private var mBufferBitmap: Bitmap? = null
    private var mBufferCanvas: Canvas? = null
    var isCanWrite = false
    private var penWidth = 2f
    var mHeight:Int = 0
    var mWidth:Int = 0

    var mPHeight:Double = 284.9879957199097
    var mPWidth:Double = 202.09933029810588
    companion object {
        const val EDIT_MODE_PEN = 0x1L      //画笔模式
        const val EDIT_MODE_ERASER = 0x2L   //橡皮擦模式
    }

    @Retention(AnnotationRetention.SOURCE)
    @LongDef(EDIT_MODE_PEN, EDIT_MODE_ERASER)
    annotation class EditMode

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (mBufferCanvas == null) {
            mBufferBitmap =
                Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888)
            //canvas绘制的内容，将会在这个mBufferBitmap内
            mBufferCanvas = Canvas(mBufferBitmap!!)
            mBufferCanvas?.drawFilter = PaintFlagsDrawFilter(0,Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
        }
    }

    //画笔
    @EditMode
    var mMode: Long = EDIT_MODE_PEN

    //想要绘制额内容先绘制到这个增加的canvas对应的bitmap上
    //写完后再把这个bitmap的ARGB信息一次提交给上下文的Canvas去绘制
    private var mPen: BasePen? = null

    init {
        mPen = NormalPen(context)
        mPen?.penWidth = 2f
    }
    fun setPenColor(color:Int){
        mPen?.penColor = ColorUtil.int2Hex(context.color(color))
    }
    fun setPenWidth(penWidth:Float){
        this.penWidth = penWidth
    }
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mHeight= h
        mWidth = w
    }
    override fun onTouchEvent(event: MotionEvent): Boolean {
        val obtain = MotionEvent.obtain(event)
        val pressure = obtain.pressure.toInt()
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mPen?.onDown(event.x,event.y,pressure,mBufferCanvas)
            }
            MotionEvent.ACTION_MOVE -> {
                mPen?.onMove(event.x,event.y,pressure,mBufferCanvas)
            }
            MotionEvent.ACTION_UP -> {
                mPen?.onUp(event.x,event.y,pressure,mBufferCanvas)
            }
        }
        mPen?.draws(mBufferCanvas)
        invalidate()
        return isCanWrite
    }

    fun processDot(pointx: Float, pointY: Float, force: Int, dotType: Dot.DotType) {
        if(!isCanWrite)return
        when(dotType){
            Dot.DotType.PEN_DOWN -> {
                mPen?.onDown(pointx, pointY, force, mBufferCanvas)
            }
            Dot.DotType.PEN_MOVE -> {
                mPen?.onMove(pointx, pointY, force, mBufferCanvas)
            }
            Dot.DotType.PEN_UP -> {
                mPen?.onUp(pointx, pointY, force, mBufferCanvas)
            }
        }
        mPen?.draws(mBufferCanvas)
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawFilter = PaintFlagsDrawFilter(0,Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
        canvas.drawBitmap(mBufferBitmap!!,0f,0f,null)
    }

    fun setMode(@EditMode mode: Long) {
        mMode = mode
        when (mode) {
            EDIT_MODE_PEN -> {
                mPen?.penWidth = penWidth
                mPen?.paint?.xfermode = null
            }
            EDIT_MODE_ERASER -> {
                mPen?.penWidth = 20f
                mPen?.paint?.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
            }
        }
    }

    /**
     * 清空画布
     */
    fun clear() {
        mBufferCanvas?.drawColor(0, PorterDuff.Mode.CLEAR)
        invalidate()
    }
     fun processEachDot(dot: Dot) {
        if (dot.force < 0) return
        val BookID = dot.BookID
        val PageID = dot.PageID
        if (BookID < 0 || PageID < 0) return //防止笔连接不切页

        //拼接dot点
        val x: Float = DotUtils.joiningTogether(dot.x, dot.fx)
        val y: Float = DotUtils.joiningTogether(dot.y, dot.fy)
        val pointX: Float = DotUtils.getPoint(
            x,
            this.mWidth,
            this.mPWidth,
            DotUtils.DIST_PERUNIT
        )
        val pointY: Float = DotUtils.getPoint(
            y,
            this.mHeight,
            this.mPHeight,
            DotUtils.DIST_PERUNIT
        )
        val pointZ = dot.force
        if (pointZ > 0) {
            this.processDot(pointX, pointY, pointZ, dot.type)
        } else if (dot.type == Dot.DotType.PEN_UP) {
            this.processDot(pointX, pointY, pointZ, Dot.DotType.PEN_UP)
        }
    }

}